package com.holly.unit.deform.api.enums;

import cn.hutool.core.util.ObjectUtil;

/**
 * 类描述: 表单设计器校验规则类型枚举类
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/2 8:14
 */
public enum CheckRuleTypeEnum {

    STRING("string", "字符串"),
    NUMBER("number", "数字"),
    BOOLEAN("boolean", "布尔值"),
    INTEGER("integer", "整数"),
    FLOAT("float", "浮点数"),
    URL("url", "URL地址"),
    EMAIL("email", "邮箱地址"),
    PHONE("phone", "手机号码"),
    IDENTITY("identity", "身份证号"),
    HEX("hex", "十六进制");

    private String type;
    private String name;

    CheckRuleTypeEnum(String type, String name) {
        this.type = type;
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public String getName() {
        return name;
    }

    /**
     * 比较两个值是否相等
     */
    public boolean compare(Object value) {
        CheckRuleTypeEnum type = CheckRuleTypeEnum.getByValue(value);
        return type == this;
    }

    public static CheckRuleTypeEnum getByValue(Object value) {
        if (ObjectUtil.isEmpty(value)) {
            return null;
        }
        return getByValue(value.toString());
    }

    public static CheckRuleTypeEnum getByValue(String value) {
        if (ObjectUtil.isEmpty(value)) {
            return null;
        }
        for (CheckRuleTypeEnum val : values()) {
            if (val.getType().toLowerCase().equals(value.toLowerCase())) {
                return val;
            }
        }
        return null;
    }

}
